//读取我的药品订单
var ws, uid, datalist, defaultIndex = 1;
if(window.plus){
	ready();
}else{
	document.addEventListener('plusready' , ready, false); 
}
function ready(){
	ws = plus.webview.currentWebview();
	uid = plus.storage.getItem('uid');
	console.log(uid);
	showWating();
//	console.log(apiRoot+"/home/drugorder/MyDrugsOrder/uid/"+uid);
	$.ajax({
		type:"get",
		url:apiRoot+"/home/drugorder/MyDrugsOrder", 
		data :{uid :uid},
		dataType :'json',
		success :function(data){
			plus.nativeUI.closeWaiting();
			datalist = data;
			showList(datalist.ing, 1);
		},
		error :function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
	//点击切换 
	$('#segmentedControl a').on('tap',function(){
		if(defaultIndex == parseInt($(this).index()+1)){
			return;
		}
		defaultIndex = parseInt($(this).index()+1);
		switch(defaultIndex){
			case 1:
				showList(datalist.ing, 1);
			break;
			default:
				showList(datalist.over, 2);
			break;			
		}
	})
}
//显示列表
function showList(arr, number){
	var str1 = '';
	$.each(arr, function(k, v) {
		str1 += '<ul class="mui-table-view del'+v.aid+'">';
			$.each(v.drug, function(key, value) {
				str1 += '<li class="mui-table-view-cell mui-media godetail" aid="'+v.aid+'">'+
			         '<a href="javascript:;">'+
			             '<img class="mui-media-object mui-pull-left" src="'+getAvatar(value.photo)+'">'+
			             '<div class="mui-media-body">'+
			                 value.title+
			                 '<p class="mui-ellipsis">¥'+value.price+'/g<span class="mui-pull-right rightnub">×'+value.num+'</span></p>'+
			             '</div>'+
			         '</a>'+
			     '</li>';
			});
	str1 += '<li class="mui-table-view-cell mui-media">'+
		        '<span>合计： ¥'+parseFloat(v.total).toFixed(2)+'</span><button onclick="operate(\''+v.aid+'\', \''+number+'\')" type="button" class="mui-btn mui-pull-right">'+
				(number==1 ? '确认收货' : '删除订单')+
				'</button>'+
		    '</li>'+
		 '</ul>';
	});
	$('#item'+number).html(str1);
	$('.godetail').on('tap', function(){
		var aid = $(this).attr('aid');
		goNewPage('me-centre-deo-details.html',{aid:aid})
	})
}
//操作
function operate(id, number){
	console.log(number);
	var action = 'receipt';
	if(number == 1){	//确认收货
		action = 'receipt';
		var status = 2;
	}else{	//删除订单
		action = 'delOrder';
		var status = 'uissee';
	}
	console.log(status);
	plus.nativeUI.confirm('确认执行此操作，此操作不可逆', function(e){
		if(e.index == 1){
			$.ajax({
				type:"get",
				url:apiRoot+"/home/drugorder/"+action,
				data :{aid :id, status:status},
				success :function(data){
					if(data > 0){
						toast('操作成功');
						if(number == 1){	//确认收货
							ws.reload();
						}else{
							$('.del'+id).remove();
							removeByValue(datalist.over, id);
						}
					}
				},
				error :function(e){
					console.log(JSON.stringify(e));
					errortoast();
				}
			});
		}
	}, null, ['取消', '确认']);
}
function removeByValue(arr, val) {
	for(var i = 0; i < arr.length; i++) {
		if(arr[i]['aid'] == val) {
			arr.splice(i, 1);
			break;
		}
	}
}